package device

import (
	"context"
	"device-admin/internal/dal"
	"device-admin/internal/dao"
	"device-admin/internal/define"
	"device-admin/internal/models"
)

type UserDevice struct{}

// Set 用户设备-设置
// userID 用户id
// robotIDs 设备id
func (UserDevice) Set(ctx context.Context, userID string, robotIDs []string) ([]string, error) {
	// 先删除原来的关联
	dao.ObjAssociation{}.Delete(ctx, dal.Q, define.AssociationUserDevie, userID, "")
	records := []*models.ObjAssociation{}
	for _, robotID := range robotIDs {
		records = append(records, &models.ObjAssociation{
			AssociationType: int(define.AssociationUserDevie),
			ObjID:           userID,
			AssociationID:   robotID,
		})
	}
	return dao.ObjAssociation{}.Add(ctx, dal.Q, records, 100, nil, dao.Device{}.Exists)
}

// Get 用户设备-获取
// userID 用户id
func (UserDevice) Get(ctx context.Context, userID string) []string {
	userIDs, err := dao.ObjAssociation{}.GetAssociationID(ctx, dal.Q, define.AssociationUserDevie, userID, "")
	if err != nil {
		return []string{}
	}
	return userIDs
}
